package com.laboratory.servicecustomer.setting.controller;

import com.Laboratory.setting.service._equipmentService;
import com.Laboratory.utilPojo.equipmentWithRoomNo;
import com.Laboratory.workBench.pojo.equipment;
import com.laboratory.servicecustomer.constant.constantArgs;
import com.laboratory.servicecustomer.utilPojo.comm;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author acok
 * @description:TODO
 * @Date 2023/1/3 10:10
 * @Version 1.0
 */
@Controller
@RequestMapping("/_equipment")
public class _equipmentController {
    @DubboReference(interfaceClass = _equipmentService.class,version = "1.0.0")
    private _equipmentService _equipmentService;

    @RequiresRoles(value = {"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:equipmentManage","Root:highest"},logical = Logical.OR)
    @PostMapping("/getEquipmentByCondition")
    @ResponseBody
    public Object getEquipmentByCondition(@RequestParam(name = "equipmentName",required = false) String equipmentName,
                                          @RequestParam(name = "roomId",required = false) String roomId,
                                          @RequestParam(value = "currentPage") Integer currentPage,
                                          @RequestParam(value = "pageSize") Integer pageSize){
        comm comm=new comm();
        Map<String,Object> map= new HashMap<>();
        map.put("equipmentName",equipmentName);
        map.put("roomId",roomId);
        map.put("pageNo",(currentPage-1)*pageSize);
        map.put("pageSize",pageSize);
        try {
            Map<String,Object> resultMap=new HashMap<>();
            List<equipmentWithRoomNo> equipments = _equipmentService.getEquipmentByCondition(map);
            int rows = _equipmentService.getEquipmentRowsByCondition(map);
            resultMap.put("equipments",equipments);
            resultMap.put("rows",rows);
            comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
            comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            comm.setOther(resultMap);
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value = {"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:equipmentManage","Root:highest"},logical = Logical.OR)
    @PostMapping("/createEquipmentRecord")
    @ResponseBody
    public Object createEquipmentRecord(@RequestBody equipment equipment){
        comm comm=new comm();
        try {
            if(_equipmentService.insertEquipment(equipment)>0){
                comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
                comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            }else {
                comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
                comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
            }
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value = {"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:equipmentManage","Root:highest"},logical = Logical.OR)
    @PostMapping("/updateEquipmentById")
    @ResponseBody
    public Object updateEquipmentById(@RequestBody equipment equipment){
        comm comm=new comm();
        try{
            if(_equipmentService.updateEquipmentById(equipment)>0) {
                comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
                comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            }else {
                comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
                comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
            }
        }catch (Exception e) {
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value = {"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:equipmentManage","Root:highest"},logical = Logical.OR)
    @PostMapping("/deleteEquipmentByIds")
    @ResponseBody
    public Object deleteEquipmentByIds(String []ids){
        comm comm=new comm();
        try{
            if(_equipmentService.deleteEquipmentByIds(ids)== ids.length) {
                comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
                comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            }else {
                comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
                comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE+"请检查选中设备是否存在报修记录！请先处理！");
            }
        }catch (Exception e) {
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value = {"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:equipmentManage","Root:highest"},logical = Logical.OR)
    @PostMapping("/updateEquipmentStateByNowTime")
    @ResponseBody
    public Object updateEquStateByOverTime(@RequestParam("nowTime") String nowTime){
        comm comm=new comm();
        try {
                _equipmentService.updateEquipmentStateByOverTime(nowTime);
                comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
                comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }
}
